/***
 * @description 地址数据库数据操作
 * @author zhang
 */

const Address = require('../models/Address')

/**
 * 创建地址
 * @param {String} username  用户名
 * @param {Object} data 地址集合
 * @returns getAddress
 */
const address = async (username, data) => {
    const getAddress = await Address.create({
        username,
        ...data
    })

    return getAddress
}




/**
 * 获取收货地址列表
 * @param {String} username 
 * @returns getAddressList
 */
const addressList = async (username) => {
    const getAddressList = await Address.find({username}).sort({ updatedAt: -1 })

    return getAddressList
}


/**
 * 获取单个收货地址
 * @param {String} id 
 * @returns address
 */
const getaddress = async (id) => {
    const address = await Address.findById(id)

    return address
}



/**
 * 更新收货地址
 * @param {String} username 
 * @param {Object} data 
 * @param {String} id 
 * @returns newAddress
 */
const updataAddress = async (username, data, id) => {
    const newAddress = await Address.findByIdAndUpdate(
        { _id: id, username },
        {username, ...data},
        { new: true }
    )

    return newAddress
}

module.exports = {
    address,
    addressList,
    getaddress,
    updataAddress
}